// guard.txt
// The default script for guards in towns. Functions much like basicnpc, but
// the creature has guard emoptes. Also, can be set to patrol out to a certain 
// point and back.
// Memory Cells:
//   Cell 0 - How creature moves.
//     0 - If 0, wander randomly. 
//     1 - Stands still until a target appears.
//     2 - Completely immobile, even if target appears.
//	   3 - Patrol guard. Goes to nav point in cell 4, and back.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this 
//     is killed, set to 1. (Example: If cell 1 is 3 and cell 2 is 5, when
//     creature is killed, sets SDF(3,5) to 1.)
//   Cell 3 - Dialogue node to start with if talked to. if left at 0, this
//     character doesn't talk.
//	 Cell 4 - The number of the nav point this guard goes to and back from if
//     patroller
//   Cell 5 - If 0, plays the bubble text.

begincreaturescript;

variables;

short i,target;
short go_out_or_in = 0;
short which_talk;

body;

beginstate INIT_STATE;
	// remove for av3###
	if (which_town() == 24) { //portal fort
			set_name(ME,"Tower Guard");
			}
		//else if (which_town() == 3) { //ToM
			//set_name(ME,"Tower Guard");
			//}
	which_talk = get_ran(1,0,1);
			
	if (get_memory_cell(0) == 3)
		set_act_at_dist(ME,1);
	if (get_memory_cell(0) == 2)
		set_walk_speed(ME,0);
	break;

beginstate DEAD_STATE;
	// Set the appropriate stuff done flag for this character being dead
	if ((get_memory_cell(1) != 0) || (get_memory_cell(2) != 0))
		set_flag(get_memory_cell(1),get_memory_cell(2),1);
break;

beginstate START_STATE; 
		
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	// Look for a target, attack it if visible
	if (get_foe_target(ME,12,0)) {
		if (dist_to_party() > 8)
			approach_char(ME,30000,7);
			else {
				do_attack();
				set_state(3);
				}
		}
		
	// Have I been hit? Strike back!
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

	// if we're hostile, hunt party down
	if ((get_attitude(ME) >= 10) && (dist_to_party() <= 40)) {
		set_foe_target(ME,random_group_member(0));
		set_state(3);
		}

	if ((get_attitude(ME) < 10) && (get_memory_cell(5) == 0)) {
		if ((get_memory_cell(0) == 1) && (get_ran(1,0,100) < 5))
			give_char_text_bubble(ME,"Zzzz.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Yawn.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"So bored.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Keep in line.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"I'm watching you.");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Did you hear something?");
		if (get_ran(1,0,100) < 3)
			give_char_text_bubble(ME,"Time for a break.");
		}
		
	// Otherwise, just peacefully move around. Go back to start, if I'm too far
	// from where I started.
	if (get_memory_cell(0) == 3) {
		if (go_out_or_in == 0) {
			 if (dist_to_nav_point(ME,get_memory_cell(4)) <= 2)
			 	go_out_or_in = 1;
			 	else if (get_ran(1,0,100) < 20) 
			 		approach_nav_point(ME,get_memory_cell(4),2);
			 }
			else {
				if (my_dist_from_start() <= 2)
					go_out_or_in = 0;
					else if (get_ran(1,0,100) < 20)
						return_to_start(ME,1);
				}
		}
		else if ((my_dist_from_start() >= 6) || ((my_dist_from_start() > 0) && (get_memory_cell(0) > 0))) {
			if (get_ran(1,1,100) < 40) 
				return_to_start(ME,1);
			}
			else if (get_memory_cell(0) == 0) {
				fidget(ME,25);
				}

	// if we're in combat and the above didn't give me anything to do, just
	// stop now. Otherwise, game will keep running script, and that eats up CPU time.
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	do_attack();
break;

beginstate TALKING_STATE;
	if (get_memory_cell(3) == 0) {
		if (which_town() == 40) { //ft draco ### remove in av3
			begin_talk_mode(10);
			end();
			}
		else if (which_town() == 43) { //new for
			begin_talk_mode(5);
			}
		else if (which_town() == 0) { //kri
			begin_talk_mode(10);
			}
		else if (which_town() == 16) { //gale
			begin_talk_mode(5);
			}

			else {
				begin_talk_mode(8005 + which_talk);
				end();
				}
		}
	begin_talk_mode(get_memory_cell(3));
break;